package com.xxyy.mylive.ui.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;

import com.astuetz.PagerSlidingTabStrip;
import com.xxyy.mylive.R;
import com.xxyy.mylive.tools.UiTool;
import com.xxyy.mylive.ui.BaseActivity;
import com.xxyy.mylive.ui.fragment.HomeFragment;

public class Home2Activity extends BaseActivity implements Toolbar.OnMenuItemClickListener {
    private Toolbar mToolbar;
    private ViewPager mViewPager;
    private PagerSlidingTabStrip tabs;

    @Override
    protected void setContentView() {
        setContentView(R.layout.activity_home2);
    }

    @Override
    protected void initUI() {
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        //applogo
        mToolbar.setLogo(R.mipmap.ic_launcher);
        mToolbar.inflateMenu(R.menu.activity_home_toobar);
        mViewPager = (ViewPager) findViewById(R.id.view_pager);
        mViewPager.setAdapter(new TabAdapter(getSupportFragmentManager()));
        tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
        tabs.setViewPager(mViewPager);
    }

    @Override
    protected void setListener() {
        mToolbar.setOnMenuItemClickListener(this);
        tabs.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                tabs.setTextColorResource(R.color.common_black);

            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    @Override
    protected void initData() {

    }

    private class TabAdapter extends FragmentPagerAdapter implements PagerSlidingTabStrip.IconTabProvider {
        private final int[] titles = {R.drawable.twitter, R.drawable.dropbox
                , R.drawable.app_store,R.drawable.edge};

        public TabAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int position) {
            return new HomeFragment();
        }

        @Override
        public int getPageIconResId(int position) {
            return titles[position];
        }

        @Override
        public CharSequence getPageTitle(int position) {
            return super.getPageTitle(position);
        }

        @Override
        public int getCount() {
            return titles.length;
        }
    }

    @Override
    public boolean onMenuItemClick(MenuItem item) {
        switch (item.getItemId()) {
            case R.id.action_search:
                //搜索
                UiTool.showToast(this, "搜索");
                break;
        }
        return true;
    }
}
